using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.VisualStudio.DebuggerVisualizers;
using Microsoft.SharePoint;
using System.Data;

namespace SPDV
{
    public class SPFieldsObjectProvider : VisualizerObjectSource
    {
        public override void GetData(object target, System.IO.Stream outgoingData)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            if(target is SPFieldCollection)
                formatter.Serialize(outgoingData, GetSerializableObject((SPFieldCollection)target));
            else
                if (target is SPList)
                    formatter.Serialize(outgoingData, GetSerializableObject((SPList)target));
        }
        private SPFieldsData GetSerializableObject(SPFieldCollection fields)
        {
            SPFieldsData fd = new SPFieldsData();
            foreach (SPField field in fields)
            {
                SPFieldData f = new SPFieldData();
                f.Title = field.Title;
                f.AuthoringInfo = field.AuthoringInfo;
                f.CanBeDeleted = field.CanBeDeleted;
                f.CanToggleHidden = field.CanToggleHidden;
                f.DefaultFormula = field.DefaultFormula;
                f.DefaultValue = field.DefaultValue;
                f.Description = field.Description;
                f.Group = field.Group;
                f.Hidden = field.Hidden;
                f.Id = field.Id;
                f.InternalName = field.InternalName;
                f.PushChangesToLists = field.PushChangesToLists;
                f.ReadOnlyField = field.ReadOnlyField;
                f.Required = field.Required;
                f.Sealed = field.Sealed;
                f.SourceId = field.SourceId;
                f.StaticName = field.StaticName;
                f.TypeAsString = field.TypeAsString;
                f.TypeDisplayName = field.TypeDisplayName;
                fd.Fields.Add(f);
            }
            return fd;
        }
        private SPFieldsData GetSerializableObject(SPList list)
        {
            return GetSerializableObject(list.Fields);
        }
    }
}
